
#!/usr/bin/env python
#import roslib; roslib.load_manifest('ebotMain')
import serial, time
import lib_M5e as M5e

# Open text file to store tag IDs and RSSI values
tag_data = open('tag_data.txt','w')
tag_data.write('Timestamp, Location, Antenna, Tag ID, RSSI Value \n')

# Open M5e reader serial connection
r = M5e.M5e('/dev/ttyUSB1', readPwr=3000)

# Move to a location
loc = 1;

# Gather tag data from port 1
r.ChangeAntennaPorts(1,1)
q1 = r.QueryEnvironment()
print 'Tags read on 1:'
for tagdata in q1:
	print tagdata[0].encode('hex')  + ' ' + str(tagdata[1])
	now = str(time.time())
	tag_data.write(now + ', ' + str(loc) + ', ' + '1' + ', ' + tagdata[0].encode('hex')  + ', ' + str(tagdata[1]) + '\n')

# Gather tag data from port 2
r.ChangeAntennaPorts(2,2)
q2 = r.QueryEnvironment()
print 'Tags read on 2:'
for tagdata in q2:
	print tagdata[0].encode('hex')  + ' ' + str(tagdata[1])
	now = str(time.time())
	tag_data.write(now + ', ' + str(loc) + ', ' + '2' + ', ' + tagdata[0].encode('hex')  + ', ' + str(tagdata[1]) + '\n')


# Save to tag_data.txt
print 'Saved to tag_data.txt'


